
var charts = [], EC = null;
var envetType =2 ;

    var dayMap={1:"周一",2:"周二",3:"周三",4:"周四",5:"周五",6:"周六",0:"周日"};
    var datepicker_op={
        format: "yyyy-mm-dd",
        clearBtn: false,
        language: "zh-CN",
        autoclose: true,
        todayHighlight: true,
        minViewMode:0,
        orientation: "top left"
    };

    function getDate(yyyymmdd){
        if(yyyymmdd.length==10){        //yyyy-MM-dd
            return Date.parse(yyyymmdd.substr(0,4)+'/'+yyyymmdd.substr(5,2)+'/'+yyyymmdd.substr(8,2));
        }else if(yyyymmdd.length==8){
            return Date.parse(yyyymmdd.substr(0,4)+'/'+yyyymmdd.substr(4,2)+'/'+yyyymmdd.substr(6,2));
        }else if(yyyymmdd.length==6){
            return Date.parse(yyyymmdd.substr(0,4)+'/'+yyyymmdd.substr(4,2));
        }
    }

    //获取星期几 dayDate 的格式为 yyyy-MM-dd
    function getWeekDay(dayDate){
        var jsDate = new Date(Date.parse(dayDate.replace(/\-/g,'/')));
        var weekNumber = jsDate.getDay();
        return dayMap[weekNumber];
    }

    function setDatePicker(ec,dateGrp,input){
        if('week_begin'==dateGrp){
        }else if('month_num'==dateGrp){
            datepicker_op.minViewMode=1;
            datepicker_op.format='yyyy-mm';
        }else{
            datepicker_op.minViewMode=0;
            datepicker_op.format='yyyy-mm-dd';
        }
        input.datepicker('remove');
        input.datepicker(datepicker_op);
        //获取显示的日期数组
        var days_show=ec.getShowDays();
        var days_all=ec.getAllDays();
        //设置时间可选范围
        input.datepicker('setStartDate',new Date(getDate(days_all[0])));
        input.datepicker('setEndDate',new Date(getDate(days_all[days_all.length-days_show.length])));
    }

    function changeDateGrp(item){
        var dateGrpVal = $(item).attr('dateGrp');
        var curDiv = $(item).closest('div[dateGrp]');
        $(curDiv).attr('dateGrp',dateGrpVal);
    }

    function changeIndex(item){
        var indexVal = $(item).val();
        var curDiv = $(item).closest('div[dateGrp]');
        $(curDiv).attr('indexType',indexVal);
    }




    //格式化输出数字
    function format_number(n){
        var b=parseInt(n).toString(); 
        var len=b.length; 
        if(len<=3){return b;} 
        var r=len%3; 
        return r>0?b.slice(0,r)+","+b.slice(r,len).match(/\d{3}/g).join(","):b.slice(r,len).match(/\d{3}/g).join(","); 
    };

    $(window).resize(function(){
        $.each(charts,function(i,n){
            n.resize();
        });
    });


    function addchvalues(value){
            var chartvalues = $("#chartvalues").val();
            var fs = chartvalues.split(",");
            var newfs ="";
            var flag = false;
            for(var i=0;i<fs.length;i++){
                var f = fs[i];
                if(f!=""){
                    if(f == value){
                      flag = true;
                      continue;
                    }
                    newfs = newfs + f+",";
                }
            }
            if(!flag){
                newfs = newfs + value+",";
            }
            $("#chartvalues").val(newfs);
    }




// 路径配置
require.config({paths: {echarts: "/echarts2/dist"}});
function getLineChart(){
    require([
            'echarts',
            'echarts/chart/line',
            'echarts/chart/bar'
        ],
        function (ec,line,bar) {
            var myChart = ec.init(document.getElementById('chartdiv'));
            var zrnumber = require('echarts/util/number');
            var zrUtil = require('zrender/tool/util');
            var option_day = {
                tooltip : {
                    trigger: 'axis',
                    formatter:tooltipFunc
                },
                legend: {
                    data:['新增有效信息量','新增有效信息量人数']
                },
                toolbox: {
                    show : true,
                    orient:'vertical',
                    x: 'right',
                    y: 'bottom',
                    feature : {
                        mark : {show: true},
                        dataZoom : {show: true},
                        magicType : {show: true, type: ['line', 'bar', 'stack', 'tiled']},
                        restore : {show: true},
                        saveAsImage : {show: true}
                    }
                },
                calculable : true,
                grid: {
                    y:30,
                    y2:50,
                    x:100,
                    x2:80
                },
                dataZoom: {
                    show: true,
                    start: 90
                },                
                xAxis: [{
                    type: "category",
                    boundaryGap: true,
                    data:[],
                    axisLabel:{
                        formatter:function(value){
                        return value;
                        }
                    }
                }],
                yAxis: [
                    {
                        type : 'value',
                        splitNumber:5
                    }
                ],
                series: [
                    {
                        name: '新增有效信息量',
                        yAxisIndex: 0,
                        type: 'line',
                        smooth: true,
                        symbol:"none",
                        data:[]
                    }
                ]
            };


           //时间轴提示框
            function  tooltipFunc(params, ticket, callback){
                var dateVal = params[0].name;
                var index_current=getIndexByName(option_day.xAxis[0].data,params[0].name);

                var result='<table class="table-center"';
                result+='<tr><td>'+dateVal+'</td><td>';
                var isStatDay = isDay(dateVal);
                if(isStatDay)
                    result += getWeekDay(dateVal);
                result += '</td>';
                if(isStatDay){
                    result +="<td>环比</td><td>周同比</td>";
                }else{
                    result += "<td>环比</td>";
                }
                result += '</tr>';

                $(params).each(function(index,para){
                    result+='<tr><td>'+para.seriesName+'</td><td>'+zrnumber.addCommas(para.data)+'</td>';
                    var seriesIndex = para.seriesIndex;
                    var day_td="<td>--</td>";
                    //环比
                    if(index_current-1>=0){
                        var data_yesday = null;
                        data_yesday= option_day.series[seriesIndex].data[index_current-1];
                        if(data_yesday != null){
                            var s=(((para.data/data_yesday)-1)*100).toFixed(2);
                            if(s>0){
                                day_td='<td style="color: orangered;">'+s+'%&#8593</td>';
                            }else{
                                day_td='<td style="color: greenyellow;">'+s+'%&#8595</td>';
                            }
                        }
                    }
                    result += day_td;
                    //周同比
                    if(isStatDay){
                        var week_td='<td>--</td>';
                        if(index_current-7>=0){
                            data_week= option_day.series[seriesIndex].data[index_current-7];
                            var s=(((para.data/data_week)-1)*100).toFixed(2);
                            if(s>0){
                                week_td='<td style="color: orangered;">'+s+'%&#8593</td>';
                            }else{
                                week_td='<td style="color: greenyellow;">'+s+'%&#8595</td>';
                            }
                        }
                        result += week_td;
                    }
                    result += '</tr>';
                });
                result+='</table>';
                return result;
            }

           //X轴不是时间轴提示框
            function  tooltipFunc_noTime(params, ticket, callback){
                var xAxisVal = params[0].name;
                var result='<table class="table-center"';
                result+='<tr><td>'+xAxisVal+'</td><td></td></tr>';

                $(params).each(function(index,para){
                    result+='<tr><td>'+para.seriesName+'</td><td>'+zrnumber.addCommas(para.data)+'</td></tr>';                
                });
                result+='</table>';
                return result;
            }

            function isDay(dateValue){
                var reg = /^(\d{1,4})(-|\/)?(\d{1,2})\2(\d{1,2})$/;     
                return reg.test(dateValue);
            }

            function isMonth(){
                var reg = /^(\d{1,4})(-|\/)?(\d{1,2})$/;   
                return reg.test(dateValue);
            }


            function getIndexByName(data,name) {
                var dataLength = data.length;
                for (var i = 0; i < dataLength; i++) {
                    if (data[i] == name || typeof data[i].value != 'undefined' && data[i].value == name) {
                        return i;
                    }
                }
                return -1;
            }

            function formatTime(time){
                return time;
            }

            function getAllDays(){
                return option_day.xAxis.data||option_day.xAxis[0].data;
            }


            /**
             * 根据指标显示日数据
             * @param eventType= 2:第一次加载, 1:切换日，周，月刷新 , 0: 添加对比后又重新选择指标
       
             */
            function show(eventType){
                var formname = $("#formname").val();                
                var formData1 = transToJSON($('#'+formname).serializeArray());
                var formData2 = transToJSON($('#'+formname+"_chart").serializeArray()); 
                // console.log(JSON.stringify(formData1));
                // console.log(JSON.stringify(formData2));
                var formData = $.extend({}, formData1,formData2);
                formData.chartType=1;  //1曲线图，2饼图    
                // console.log(JSON.stringify(formData));             
                myChart.showLoading({
                    text: '正在努力的读取数据中...'
                });               

                $.ajax({
                    url: "/intreport/chartsearch/"+pathId,
                    type: 'post',
                    async: true,
                    data:  formData,
                    dataType: 'json',
                    timeout:60000,
                    success: function(data, textStatus) {
                        //图例
                        option_day.legend.data = data.legendData;
                        //X轴
                        option_day.xAxis[0].data = data.xAxisData;
                        //Y轴
                        var yAxisCount = data.yAxisCount;
                        option_day.yAxis = [];                        
                        for (var i = 1 ; i <= yAxisCount; i++) {
                            var tmp = {type : 'value',splitNumber:5};
                            option_day.yAxis.push(tmp);
                        };
                        
                        option_day.series=[];
                        //X轴是否为时间轴
                        var isTimeLine = isDay(data.xAxisData[0]);
                        if(!isTimeLine){
                            //不是时间维度, 修改提示框
                            option_day.tooltip.formatter = tooltipFunc_noTime;
                        }
                        $.each(data.yAxisIndexMap,function(name, yAxisIndex){
                             var tmp = {
                                name: name,
                                yAxisIndex: yAxisIndex,
                                type: 'line',
                                smooth: true,
                                symbol:"none",
                                data: data.lineData[name]
                            };

                            if(!isTimeLine){
                                tmp.type = 'bar';
                            }
                            option_day.series.push(tmp);
                        });

                        //游标位置
                        var datezoom_local={start:(1-31/data.xAxisData.length)*100,end:100,show: true,height: 20};
                        //小于31个点全显
                        if(data.xAxisData.length<31){
                            datezoom_local={start:0,end:100,show: true,height: 20};
                        }

                        if(eventType == 1 || eventType == 2){                            
                            if(eventType == 1 && myChart.component.dataZoom){
                                option_day.dataZoom=datezoom_local;
                                myChart.component.dataZoom.absoluteZoom(datezoom_local);
                                option_day.dataZoom.show=true;
                            }else if(eventType == 2){
                                option_day.dataZoom=datezoom_local;
                            }
                            myChart.setOption(option_day,true);
                        }else if(eventType == 0){
                            myChart.component.dataZoom=compDataZoom_old;
                            myChart.setOption(option_day,true);
                        }
                        myChart.hideLoading();
                        // rescheckgroup();
                    },
                    error : function() {
                        myChart.hideLoading();
                    }
                });
            }

            EC={
                "show":show,
                "clear": function(){ myChart.clear();},
                "resize": function(){ myChart.resize();}
            };
            $(window).resize(function(){
               myChart.resize();
            });            
            show(envetType);
            return EC;          
        }
    );
}


function getPieChart(){
    require([
            'echarts',
            'echarts/chart/pie',
            'echarts/chart/funnel'
        ], function (ec,pie,funnel) {   
            var myChart = ec.init(document.getElementById('chartdiv'));
            var zrnumber = require('echarts/util/number');
            var zrUtil = require('zrender/tool/util'); 
            var option_day = {
                tooltip : {
                    trigger: 'item',
                    formatter: "{b}<br/>{a} : {c} ({d}%)"
                },
                legend: {
                    orient : 'vertical',
                    x : 'left',
                    data:[]
                },
                toolbox: {
                    show : true,
                     orient: 'vertical',
                    feature : {
                        mark : {show: false},
                        dataView : {show: false, readOnly: false},
                        magicType : {
                            show: true, 
                            type: ['pie']
                        },
                        restore : {show: true},
                        saveAsImage : {show: true}
                    }
                },
                calculable : true,
                series : [
                    {
                        name:'总计',
                        type:'pie',
                        selectedMode: 'single',
                        radius : [0, '65%'],
                        // for funnel
                        x: '20%',
                        width: '40%',
                        funnelAlign: 'right',
                        itemStyle : {
                            normal : {
                                label : {
                                    show : true,
                                    position:'outer'
                                },
                                labelLine : {
                                    show : true
                                }
                            }
                        },
                        data:[]
                    }
                    ]
            };  
    
            function show(){
                var formname = $("#formname").val();                
                var formData1 = transToJSON($('#'+formname).serializeArray());
                var formData2 = transToJSON($('#'+formname+"_chart").serializeArray()); 
                // console.log(JSON.stringify(formData1));
                // console.log(JSON.stringify(formData2));
                var formData = $.extend({}, formData1,formData2);
                formData.chartType = 2;  //1曲线图，2饼图    
                console.log(JSON.stringify(formData));             
                myChart.showLoading({
                    text: '正在努力的读取数据中...'
                });    

                $.ajax({
                    url: "/intreport/chartsearch/"+pathId,
                    type: 'post',
                    async: true,
                    data:  formData,
                    dataType: 'json',
                    timeout:60000,
                    success: function(data, textStatus) {
                        option_day.legend.data = data.nameList;
                        option_day.series[0].data = data.pieData;
                        option_day.series[0].name = data.indexName;
                        myChart.setOption(option_day,true);
                        myChart.hideLoading();
                    },
                    error : function() {
                        myChart.hideLoading();
                    }
                });
            };

            EC={
                "show":show,
                "clear": function(){ myChart.clear();},
                "resize": function(){ myChart.resize();}
            };
            $(window).resize(function(){
               myChart.resize();
            });            
            show();
            return EC;
    });
};








/*  类别级联  */
function removeCate_chart(level,IDSuffix){
    if(level <= 3){
        if($("#cateThree_"+IDSuffix).length > 0)
            $("#cateThree_"+IDSuffix).empty();
    }
    if(level <= 2){
        if($("#cateTwo_"+IDSuffix).length > 0)
            $("#cateTwo_"+IDSuffix).empty();
    }
    if(level <= 1){
         if($("#cateOne_"+IDSuffix).length > 0)
            $("#cateOne_"+IDSuffix).empty();
    }
}

function getOptionList_chart(dataList,type, showType){
    var optionList='';
    //向上汇总
    if(showType == 3){
        optionList="<option value=''>全部</option>";
    }
    $.each(dataList, function(i, obj) {
          if(type == 1){
            optionList = optionList + "<option value='"+obj.cate1_name+"' title='"+obj.cate1_name+"'>"+obj.cate1_name+"</option>";
          }else if(type == 2){
            optionList = optionList + "<option value='"+obj.cate2_name+"' title='"+obj.cate2_name+"'>"+obj.cate2_name+"</option>";
          }else if(type == 3){
            optionList = optionList + "<option value='"+obj.cate3_name+"' title='"+obj.cate3_name+"'>"+obj.cate3_name+"</option>";
          }
          
     });
    return optionList;      
}

function changeCate_chart(level,type){
    var reqUrl = '';
    var IDSuffix = 'dp';
    var paramData='';
    if(type == "dp")
        reqUrl = "/intChartsTool/newdiscate";
    else if(type == "bc"){
        reqUrl = "/intChartsTool/newbelongcate";
        IDSuffix="bc";
    }
    var isBiz = $("#isBiz").val();
    var showType = $("#cateShowType").val();
     if(level > 3){
        return;
     }
     
    var businessType = $("#"+IDSuffix+"BusinessTypeName_chart").length > 0 ? $("#"+IDSuffix+"BusinessTypeName_chart").val() : "";
    var cate1 = $("#"+IDSuffix+"NewCateOne_chart").length > 0 ? $("#"+IDSuffix+"NewCateOne_chart").val() : "";
    var cate2 = $("#"+IDSuffix+"NewCateTwo_chart").length > 0 ? $("#"+IDSuffix+"NewCateTwo_chart").val() : "";
    if(level == 1){
         if($("#"+IDSuffix+"NewCateOne_chart").length > 0)
            paramData = {"business_type_name":String(businessType),"isBiz":String(isBiz)};
        else{
            level += 1;
        }
    }
    
    if(level == 2){
        if($("#"+IDSuffix+"NewCateTwo_chart").length > 0){
            paramData = {"cate1":String(cate1),"business_type_name":String(businessType),"isBiz":String(isBiz)};
        }
        else{
            level += 1;
        }
    }

    if(level == 3){
        if($("#"+IDSuffix+"NewCateThree_chart").length > 0){
            paramData = {"cate1":String(cate1),"cate2":String(cate2),"business_type_name":String(businessType),"isBiz":String(isBiz)};
        }
        else{
            return ;
        }
    }
    paramData.level=level;
        jQuery.ajax({
            type:"POST",
            dataType:"json",
            async:true,
            url:reqUrl,
            data:paramData,
            success:function(data){
            if(data!="" && data.result =="right"){
                var cateList='';
                if(level == 3){
                    if(type == "dp")
                        cateList = getOptionList_chart(data.disCateListThree,3, showType);
                    else if(type == "bc")
                        cateList = getOptionList_chart(data.belongCateListThree,3);
                    $("#"+IDSuffix+"NewCateThree_chart").empty().append(cateList);
                }else if(level == 2){
                    if(type == "dp")
                        cateList = getOptionList_chart(data.disCateListTwo,2, showType);
                    else if(type == "bc")
                        cateList = getOptionList_chart(data.belongCateListTwo,2, showType);
                    $("#"+IDSuffix+"NewCateTwo_chart").empty().append(cateList);                    
                    // removeCate(3,IDSuffix);
                }else if(level == 1){
                    if(type == "dp")
                        cateList = getOptionList_chart(data.disCateListOne,1, showType);
                    else if(type == "bc")
                        cateList = getOptionList_chart(data.belongCateListOne,1, showType);
                    $("#"+IDSuffix+"NewCateOne_chart").empty().append(cateList, showType);
                    // removeCate(2,IDSuffix);
                }
                changeCate(level+1,type);
           }else{
              alert("error!");
           }
        }
      });
}

//城市级联
function getGenelOptionList_chart(dataList,attr,showType,showAttr){
    if(!showAttr){
        showAttr = attr;
    }
    var optionList="";
    //向上汇总
    if(showType == 3){
        // optionList="<option value=''>全部</option>";
    }
    $.each(dataList, function(i, obj) {
            optionList = optionList + "<option value='"+obj[attr]+"' title='"+obj[showAttr]+"'>"+obj[showAttr]+"</option>";          
     });
    return optionList;      
}

function changeCity_chart(level,type){
    var reqUrl = '';
    var IDSuffix = 'dl';
    var paramData= {};
    if(type == "dl")
        reqUrl = "/intChartsTool/displaycity";
    else if(type == "bc"){
        reqUrl = "/intChartsTool/belongcity";
        IDSuffix="bl";
    }
    var isBiz = $("#isBiz").val();
    var cityShowType = $("#cityShowType").val();

     if(level > 3){
        return;
     }
    var city1 = $("#"+IDSuffix+"CityOne_chart").length > 0 ? $("#"+IDSuffix+"CityOne_chart").val() : "";
    var city2 = $("#"+IDSuffix+"CityTwo_chart").length > 0 ? $("#"+IDSuffix+"CityTwo_chart").val() : "";
    if(level == 1){
         if($("#"+IDSuffix+"CityOne_chart").length <= 0)
            level += 1;
    }
    
    if(level == 2){
        if($("#"+IDSuffix+"CityTwo_chart").length > 0){
            paramData = {"city1":String(city1)};
        }
        else{
            level += 1;
        }
    }

    if(level == 3){
        if($("#"+IDSuffix+"CityThree_chart").length > 0){
            paramData = {"city1":String(city1),"city2":String(city2)};
        }
        else{
            return;
        }
    }
    paramData.level=level;
        jQuery.ajax({
            type:"POST",
            dataType:"json",
            async:true,
            url:reqUrl,
            data:paramData,
            success:function(data){
            if(data!="" && data.result =="right"){
                var cityList='';
                if(level == 3){
                    if(type == "dl")
                        cityList = getGenelOptionList_chart(data.disCateListThree, "localName",cityShowType);
                    else if(type == "bl")
                        cityList = getGenelOptionList_chart(data.belongCateListThree, "localName",cityShowType);
                    $("#"+IDSuffix+"CityThree_chart").empty().append(cityList);
                }else if(level == 2){
                    if(type == "dl")
                        cityList = getGenelOptionList_chart(data.disCateListTwo, "localName",cityShowType);
                    else if(type == "bl")
                        cityList = getGenelOptionList_chart(data.belongCateListTwo,"localName",cityShowType);
                    $("#"+IDSuffix+"CityTwo_chart").empty().append(cityList);                    
                    // removeCate(3,IDSuffix);
                }else if(level == 1){
                    if(type == "dl")
                        cityList = getGenelOptionList_chart(data.disCateListOne,"localName",cityShowType);
                    else if(type == "bl")
                        cityList = getGenelOptionList_chart(data.belongCateListOne,"localName",cityShowType);
                    $("#"+IDSuffix+"CityOne_chart").empty().append(cityList);
                    // removeCate(2,IDSuffix);
                }
                changeCity(level+1,type);
           }else{
              alert("error!");
           }
        }
      });
}

function changeInfoSourceName_chart(platform){
    if($("#infoSourceName_chart").length <= 0){
        return;
    }
    var infoShowType = $('#infoShowType').val();
    var localVar = String(platform);
    var optionList = '';
    jQuery.ajax({
        type:"post",
        dataType:"json",
        async:true,
        url:"/intChartsTool/infosourceSearch",
        data:{"infoPlatform":localVar},
        success:function(data){
           if(data!="" && data.result =="right"){               
                optionList = getGenelOptionList_chart(data.infoSourceList,"sourceName",infoShowType);
                $("#infoSourceName_chart").empty().append(optionList);
           }else{
               alert("error!");
           }
        }
   });
}


function changeFlowSource_chart(level){
     if(level > 4 || level <=1){
        return;
     }
    var reqUrl = "/intChartsTool/flowsourcesearch";   
    var showType = $("#flowShowType").val();

    var flowSourcePlatform = $("#flowSourcePlatform").length > 0 ? $("#flowSourcePlatform").val() : "";
    var source1 = $("#flowSourceOne_chart").length > 0 ? $("#flowSourceOne_chart").val() : "";
    var source2 = $("#flowSourceTwo_chart").length > 0 ? $("#flowSourceTwo_chart").val() : "";
    var source3 = $("#flowSourceThree_chart").length > 0 ? $("#flowSourceThree_chart").val() : "";
    var source4 = $("#flowSourceFour_chart").length > 0 ? $("#flowSourceFour_chart").val() : "";    
    if(level == 2){
        if($("#flowSourceTwo_chart").length > 0){
            paramData = {"flowSourcePlatform":flowSourcePlatform, "source1":source1};
        }
        else{
            level += 1;
        }
    }
    if(level == 3){
        if($("#flowSourceThree_chart").length > 0){
           paramData = {"flowSourcePlatform":flowSourcePlatform, "source1":source2, "source2":source2};
        }
        else{
           level += 1;
        }
    }    
    if(level == 4){
        if($("#flowSourceFour_chart").length > 0){
           paramData = {"flowSourcePlatform":flowSourcePlatform, "source1":source2, "source2":source2, "source3":source3};
        } else{
           return ;
        }
    }
    paramData.level=level;
    var optionList = '';

    jQuery.ajax({
        type: "POST",
        dataType: "json",
        async: true,
        url: reqUrl,
        data: paramData,
        success:function(data){
            if(data!="" && data.result =="right"){
                if(level == 4){
                    optionList = getGenelOptionList_chart(data.flowSourceList, "source4",showType, "source4_name");
                    $("#flowSourceFour_chart").empty().append(optionList);
                }else if(level == 3){
                    optionList = getGenelOptionList_chart(data.flowSourceList, "source3",showType, "source3_name");
                    $("#flowSourceThree_chart").empty().append(optionList);                    
                }else if(level == 2){
                    optionList = getGenelOptionList_chart(data.flowSourceList, "source2",showType, "source2_name");
                    $("#flowSourceTwo_chart").empty().append(optionList);                    
                }else{
                    return;
                }
                changeFlowSource_chart(level+1);
           }else{
              alert("error!");
           }
        }
    });
}

function changeJxedtSource_chart(level){
     if(level > 2 || level <=1){
        return;
     }
    var reqUrl = "/intChartsTool/jxedtSourceSearch";   
    var showType = $("#jxedtShowType").val();

    var jxedtFlowSourcePlatform = $("#jxedtFlowSourcePlatform").length > 0 ? $("#jxedtFlowSourcePlatform").val() : "";
    var source1 = $("#jxedtSourceOne_chart").length > 0 ? $("#jxedtSourceOne_chart").val() : "";
    var source2 = $("#jxedtSourceTwo_chart").length > 0 ? $("#jxedtSourceTwo_chart").val() : "";

    if(level == 2){
        if($("#jxedtSourceTwo_chart").length > 0){
            paramData = {"jxedtFlowSourcePlatform":jxedtFlowSourcePlatform, "source1":source1};
        }
        else{
            return;
        }
    }
    
    paramData.level=level;
    paramData.pathId = $("#pathId").val();
    var optionList = '';

    jQuery.ajax({
        type: "POST",
        dataType: "json",
        async: true,
        url: reqUrl,
        data: paramData,
        success:function(data){
            if(data!="" && data.result =="right"){
                if(level == 2){
                    optionList = getGenelOptionList_chart(data.jxedtSourceList, "source2",showType, "source2");
                    $("#jxedtSourceTwo_chart").empty().append(optionList);
                }else{
                    return;
                }
                // changeJxedtSource_chart(level+1);
           }else{
              alert("error!");
           }
        }
    });
}

function transToJSON(obj){
    var d={};
     $.each(obj, function() {
      d[this.name] = this.value;
    });
     // console.log(obj);
     return d;
}

function changeCHRSource_chart(level){
    if(level > 4 || level <=1){
        return;
     }
    var reqUrl = "/intChartsTool/chrflowsourcesearch";   
    var showType = $("#chrFlowShowType").val();

    var chrFlowSourcePlatform = $("#chrFlowSourcePlatform").length > 0 ? $("#chrFlowSourcePlatform").val() : "";
    var source1 = $("#chrSourceOne_chart").length > 0 ? $("#chrSourceOne_chart").val() : "";
    var source2 = $("#chrSourceTwo_chart").length > 0 ? $("#chrSourceTwo_chart").val() : "";
    var source3 = $("#chrSourceThree_chart").length > 0 ? $("#chrSourceThree_chart").val() : "";
    // var source4 = $("#chrSourceFour_chart").length > 0 ? $("#chrSourceFour_chart").val() : "";  
    if(level == 2){
        if($("#chrSourceTwo_chart").length > 0){
            paramData = {"chrFlowSourcePlatform":chrFlowSourcePlatform, "source1":source1};
        }
        else{
            level += 1;
        }
    }
    if(level == 3){
        if($("#chrSourceThree_chart").length > 0){
           paramData = {"chrFlowSourcePlatform":chrFlowSourcePlatform, "source1":source2, "source2":source2};
        }
        else{
           level += 1;
        }
    }    
    if(level == 4){
        if($("#chrSourceFour_chart").length > 0){
           paramData = {"chrFlowSourcePlatform":chrFlowSourcePlatform, "source1":source2, "source2":source2, "source3":source3};
        } else{
           return ;
        }
    }
    paramData.level=level;
    var optionList = '';

    jQuery.ajax({
        type: "POST",
        dataType: "json",
        async: true,
        url: reqUrl,
        data: paramData,
        success:function(data){
            if(data!="" && data.result =="right"){
                if(level == 4){
                    optionList = getGenelOptionList_chart(data.chrSourceList, "source4",showType, "source4_name");
                    $("#chrSourceFour_chart").empty().append(optionList);
                }else if(level == 3){
                    optionList = getGenelOptionList_chart(data.chrSourceList, "source3",showType, "source3_name");
                    $("#chrSourceThree_chart").empty().append(optionList);                    
                }else if(level == 2){
                    optionList = getGenelOptionList_chart(data.chrSourceList, "source2",showType, "source2_name");
                    $("#chrSourceTwo_chart").empty().append(optionList);                    
                }else{
                    return;
                }
                changeFlowSource_chart(level+1);
           }else{
              alert("error!");
           }
        }
    });
}

function changeCHRIndustry_chart(level){
    if(level > 2 || level <=1){
        return;
     }
    var reqUrl = "/intChartsTool/chrindustrysearch";   
    var showType = $("#industryShowType").val();

    var source1 = $("#chrIndustryOne_chart").length > 0 ? $("#chrIndustryOne_chart").val() : "";

    // var source4 = $("#chrSourceFour_chart").length > 0 ? $("#chrSourceFour_chart").val() : "";  
    if(level == 2){
        if($("#chrIndustryTwo_chart").length > 0){
            paramData = {"source1":source1};
        }
        else{
            return ;
        }
    }
    
    paramData.level=level;
    var optionList = '';

    jQuery.ajax({
        type: "POST",
        dataType: "json",
        async: true,
        url: reqUrl,
        data: paramData,
        success:function(data){
            if(data!="" && data.result =="right"){
                if(level == 2){
                    optionList = getGenelOptionList_chart(data.chrIndustryList, "cate2_name",showType, "cate2_name");
                    $("#chrIndustryTwo_chart").empty().append(optionList);
                }else{
                    return;
                }
                // changeFlowSource_chart(level+1);
           }else{
              alert("error!");
           }
        }
    });
}

function changeCHRJob_chart(level){
    if(level > 3 || level <=1){
        return;
     }
    var reqUrl = "/intChartsTool/chrjobsearch";   
    var showType = $("#chrJobShowType").val();

    var cate1_name = $("#chrJobOne_chart").length > 0 ? $("#chrJobOne_chart").val() : "";
    var cate2_name = $("#chrJobTwo_chart").length > 0 ? $("#chrJobTwo_chart").val() : "";

    // var source4 = $("#chrSourceFour_chart").length > 0 ? $("#chrSourceFour_chart").val() : "";  
    if(level == 2){
        if($("#chrJobTwo_chart").length > 0){
            paramData = {"cate1_name":cate1_name};
        }
        else{
            level += 1 ;
        }
    }    
    if(level == 3){
        if($("#chrJobThree_chart").length > 0){
            paramData = {"cate1_name":cate1_name, "cate2_name":cate2_name, };
        }
        else{
             return;
        }
    }
    
    paramData.level=level;
    var optionList = '';

    jQuery.ajax({
        type: "POST",
        dataType: "json",
        async: true,
        url: reqUrl,
        data: paramData,
        success:function(data){
            if(data!="" && data.result =="right"){
                if(level == 2){
                    optionList = getGenelOptionList_chart(data.chrJobList, "cate2_name",showType, "cate2_name");
                    $("#chrIndustryTwo_chart").empty().append(optionList);
                }else if(level == 3){
                    optionList = getGenelOptionList_chart(data.chrJobList, "cate3_name",showType, "cate3_name");
                    $("#chrIndustryTwo_chart").empty().append(optionList);
                }else{
                    return;
                }
                // changeFlowSource_chart(level+1);
           }else{
              alert("error!");
           }
        }
    });
}

/* 绑定级联事件 */
function bindChangeEvent(){
    /**城市控件**/
    $("select[compType='city_chart']").on('change', function(){
        var selectType=$(this).attr('type');
        var selectLevel=parseInt($(this).attr('level'));
        changeCity_chart(selectLevel + 1 ,selectType);
    });

    /**类别控件**/
    $("select[compType='cate_chart']").on('change', function(){
        var selectType=$(this).attr('type');
        var name=$(this).attr('name')+"_chart";
        var selectLevel=parseInt($(this).attr('level'));
        var selected = $(this).val();
        changeCate_chart(selectLevel + 1 ,selectType); 
    });

    /**信息来源平台控件**/
    $("select[compType='infoPlatform']").on('change', function(){
        var selected = $(this).val();
        changeInfoSourceName_chart(selected);
    });

     /**流量来源控件**/
    $("select[compType='source']").on('change', function(){
        var selectLevel=parseInt($(this).attr('level'));     
        changeFlowSource_chart(selectLevel + 1);  
     });


    /**驾校一点通来源控件**/
    $("select[compType='jxedtsource']").on('change', function(){
        var selectLevel=parseInt($(this).attr('level')); 
        changeJxedtSource_chart(selectLevel + 1);
    });   

     /**中华英才来源控件**/
    $("select[compType='chrsource']").on('change', function(){
        var selectLevel=parseInt($(this).attr('level'));
        changeCHRSource_chart(selectLevel + 1);
    });
    /**中华英才行业控件**/
    $("select[compType='chrindustry']").on('change', function(){      
        var selectLevel=parseInt($(this).attr('level'));
        changeCHRIndustry_chart(selectLevel + 1);
    }); 
    /**中华职位行业控件**/
    $("select[compType='chrjob']").on('change', function(){      
        var selectLevel=parseInt($(this).attr('level'));
        changeCHRJob_chart(selectLevel + 1);
    });
}

/* 创建维度筛选下拉框 */
function createForm(){
    // var pathId  = $("#pathId").val();
    // var groupBy = $("#groupBy").val();
    var formname = $("#formname").val();
     $.ajax({
        url: "/intcharts/createForm",
        type: 'post',
        async: false,
        data: $('#'+formname).serialize(), 
        dataType: 'text',
        timeout:60000,
        success: function(data, textStatus) {
            $('#td_chart_selects').prepend(data);
             bindChangeEvent();             
        },
        error : function() {
            alert("error");
        }
    });                    
}

function setSelectValue_chart(listvalues,txtvalues){
    var liststr=listvalues;
    var resulststr="";
    for(var i=0;i<liststr.length;i++){
             resulststr +=  liststr[i]+",";
    }
     $('#'+txtvalues+'').val(resulststr.substring(0,resulststr.lastIndexOf(",")));
}

 /** 绘图指标下拉框 **/
$("select[compType='chart_index']").on('change', function(){
        var hiddenInput = $(this).parent().find("input:hidden");
        var id= hiddenInput.attr('id');
        // var select =$(this).find("option:selected").val();
        if($(this).val()) {
            if( $(this).val().length > 10){
                alert("最多只能选择10个指标哦!");
                return false;
            }
           
            setSelectValue_chart($(this).val(),id);
            if($(this).val().length == 1){
                $('#pie_chartSearch').prop("disabled", false);
            }else{
                $('#pie_chartSearch').prop("disabled", true);
            }
        }else{
            var initailVal = $('#'+id).attr("initailVal");
            $('#'+id).val(initailVal);
            $('#pie_chartSearch').prop("disabled", true);
        }
});

var initialized = false;
$("#chartview").click(function(){            
    // $('.panelhidden-bg').show();
    $("#chartType").val(1);
    envetType = 2;
    var top = window.screen.availHeight * 0.2;
    $('html, body').animate({scrollTop: top }, 'slow');
    if(!initialized){
        var gby = $("#groupBy").val();
        if(!gby){
            alert("请选择维度!");
        }
        var lastChar = gby.charAt(gby.length -1);
        var gbyCount = gby.split(",").length;
        //一个维度不用选
        if(gbyCount>2 || (lastChar != ',' && gbyCount == 2)){
            createForm();
        }else{
            $('#pie_chartSearch').show();
            if($('#chartvalues').attr("initailVal").split(",").length<=2){
                $('#pie_chartSearch').prop("disabled",false);
            }
        }
        setSelectValue_chart($("select[compType='chart_index']").val(),"chartvalues");
        getLineChart();
        initialized = true;           
    }
    $('#hoverChartDiv').show();
});

$(".close-chart").click(function(){
    // EC.clear();
    $('#hoverChartDiv').hide();
    $('.panelhidden-bg').hide();
});

$('#chartdiv').width(document.body.clientWidth*0.92);

$("#chartSearch").click(function(){
    var lineChart = getLineChart();  
});

$("#pie_chartSearch").click(function(){
    var pieChart = getPieChart();  
});